package cn.itcast.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
@Slf4j
public class FilterConfiguration {
    @Bean
    @Order(1)
    public GlobalFilter globalFilter1() {

        return ((exchange, chain) -> {
            log.info("过滤器1    前置阶段");
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                log.info("过滤器1  后置阶段");
            }));
        });
//        return new GlobalFilter() {
//            @Override
//            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//                log.info("过滤器前置阶段");
//                return chain.filter(exchange).then(Mono.fromRunnable(()->{
//                    log.info("过滤器后置阶段");
//                }));
//            }
//        };
    }

    @Bean
    @Order(2)
    public GlobalFilter globalFilter2() {

        return ((exchange, chain) -> {
            log.info("过滤器2    前置阶段");
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                log.info("过滤器2  后置阶段");
            }));
        });
    }

    @Bean
    @Order(3)
    public GlobalFilter globalFilter3() {

        return ((exchange, chain) -> {
            log.info("过滤器3    前置阶段");
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                log.info("过滤器3  后置阶段");
            }));
        });
    }
}